package com.hmall.order.config;

import com.hmall.order.constants.MqConstants;
import org.springframework.amqp.core.*;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    /**
     * 创建交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(
                MqConstants.ORDER_EXCHANGE,true,true);
    }
    @Bean
    public TopicExchange topicExchange2(){
        return new TopicExchange(MqConstants.ORDER_CLOSE_EXCHANGE,true,false);
    }

    /**
     * 创建队列: 存放新增和修改的消息
     * @return
     */
    @Bean
    public Queue payCloseQueue(){
        return new Queue(MqConstants.ORDER_CLOSE_QUEUE, true);
    }
    @Bean(name = "order.delay.queue")
    public Queue payQueue() {
        return QueueBuilder
                .durable(MqConstants.ORDER_PAY_QUEUE)
                // 配置到期后转发的交换
                .withArgument("x-dead-letter-exchange",MqConstants.ORDER_CLOSE_EXCHANGE)
                // 配置到期后转发的路由键
                .withArgument("x-dead-letter-routing-key", MqConstants.ORDER_CLOSE_KEY)
                .build();
    }
    /**
     * 将队列绑定到交换机上并设置路由key
     * @return
     */
    @Bean
    public Binding insertQueueBinding(){
        return BindingBuilder.bind(payQueue()).to(topicExchange()).with(MqConstants.ORDER_PAY_KEY);
    }
    @Bean
    public Binding insertQueueBinding2(){
        return BindingBuilder.bind(payCloseQueue()).to(topicExchange2()).with(MqConstants.ORDER_CLOSE_KEY);
    }


}